﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Resources;
using System.Globalization;

namespace atm_qlda
{
    public partial class changePass : Form
    {
        string succ;
        string err;
        string id;
        string language;
        string errorpass;
        string info;
        CultureInfo culture;
        DataSet ds;
        SqlDataAdapter da;
        DataTable dt;
        public changePass()
        {
            InitializeComponent();
            
        }

        private void changePass_Load(object sender, EventArgs e)
        {
            txtenteroldPin.Focus();
        }

        public void chuyendl2(string dl, string lag)
        {
            this.id = dl;
            this.language = lag;
        }


        public delegate void delegateSend(string text, string language);
        private void bt_ok_Click(object sender, EventArgs e)
        {            
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["conStr"];
            string sql = "SELECT * FROM Card WHERE IDCard='" + id + "'";
            dt = new DataTable();
            ds = new DataSet();
            da = new SqlDataAdapter(sql, conn);
            da.Fill(ds,"crd");
            dt = ds.Tables["crd"];
            DataRow dr = dt.Rows[0];
                  
                
                 if (txtenteroldPin.Text!=dr["PIN"].ToString() )
                 {
                     MessageBox.Show(errorpass, err);
                     txtenteroldPin.Clear();
                     txtenteroldPin.Focus();
                 }

                else if (txt_changepass1.Text == "" || txt_changepass2.Text == "" || txt_changepass1.Text != txt_changepass2.Text || txtenteroldPin.Text == "")
                     {
                         MessageBox.Show(errorpass,err,MessageBoxButtons.YesNo,MessageBoxIcon.Error);
                         txtenteroldPin.Clear();
                         txt_changepass1.Clear();
                         txt_changepass2.Clear();
                         txtenteroldPin.Focus();
                        
                     }
                 else
                 {
                   
                     string sqlup = "UPDATE Card SET PIN=@newPIN where IDCard='" + id + "'AND PIN=@PIN";

                     SqlCommand cmd = new SqlCommand(sqlup,conn);
                     cmd.Parameters.AddWithValue("@newPIN",txt_changepass1.Text);
                     cmd.Parameters.AddWithValue("@PIN",txtenteroldPin.Text);
                     conn.Open();
                     cmd.ExecuteNonQuery();
                     conn.Close();
                     if (MessageBox.Show(succ, info,MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1) == DialogResult.Yes) {
                         Choose cs = new Choose();
                         cs.SetLanguage(language);
                         cs.Show();
                         this.Hide();
                     }
                     Close();  
                 }
          }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            txt_changepass1.Clear();
            txt_changepass2.Clear();
            txtenteroldPin.Clear();
            txtenteroldPin.Focus();
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtenteroldPin_TextChanged(object sender, EventArgs e)
        {
            this.txtenteroldPin.Focus();
            if (txtenteroldPin.TextLength == 4) {
                txt_changepass1.Focus();
            }
        }

        private void txt_changepass1_TextChanged(object sender, EventArgs e)
        {
            if (txt_changepass1.TextLength == 4) {
                txt_changepass2.Focus();
            }
        }


        public void SetLanguage(string cultureName)
        {
            culture = CultureInfo.CreateSpecificCulture(cultureName);
            ResourceManager rm = new ResourceManager("atm_qlda.Lang.MyResource", typeof(changePass).Assembly);
            label7.Text = rm.GetString("bank",culture);
            label4.Text = rm.GetString("oldpass",culture);
            label2.Text = rm.GetString("newpass",culture);
            label1.Text = rm.GetString("renew",culture);
            label3.Text = rm.GetString("enter",culture);
            label6.Text = rm.GetString("bttout",culture);
            label5.Text = rm.GetString("bttback",culture);
            succ = rm.GetString("succ", culture);
            errorpass = rm.GetString("errorpass", culture);
            err = rm.GetString("err",culture);
            info = rm.GetString("info", culture);
        }

        public void sendl(string id, string lag)
        {

            this.id = id;
            this.language = lag;
        }

               
    }
}
